package com.sys.servlet.mgr.device;

import com.sys.pojo.JsonResult;
import com.sys.service.CustomerService;
import com.sys.service.DeviceService;
import com.sys.utils.WriteJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 根据id删除
 * 需要传入id
 */
@WebServlet("/mgr/device/deleteById")
public class DeleteByIdDeviceServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        PrintWriter writer = resp.getWriter();

        String did = req.getParameter("did");
        JsonResult jr = new JsonResult();

        // 将字符串转成 int类型
        int didInt = 0 ;
        try {
            didInt = Integer.parseInt(did);
        }catch (NumberFormatException e){
            e.printStackTrace();
            jr.setCode(601);
            jr.setMsg("传入数据错误");
            WriteJson.write(resp,jr);

        }
        DeviceService deviceService = new DeviceService();
        try {
            int i = deviceService.delete(didInt);

            if(i>0) {
                jr.setCode(0);
                jr.setMsg("删除成功");

            }else{
                jr.setCode(104);
                jr.setMsg("没有该名片");
            }
        } catch (Exception e) {
            e.printStackTrace();
            jr.setCode(500);
            jr.setMsg("操作发生错误");
        }
        WriteJson.write(resp,jr);
    }
}
